package com.bigstonecontainer.training.springboot.frm.server.sys.service.impl;

import com.bigstonecontainer.training.springboot.frm.server.sys.domain.SysUser;
import com.bigstonecontainer.training.springboot.frm.server.sys.domain.SysUserRole;
import com.bigstonecontainer.training.springboot.frm.server.sys.mapper.SysRoleMapper;
import com.bigstonecontainer.training.springboot.frm.server.sys.mapper.SysUserMapper;
import com.bigstonecontainer.training.springboot.frm.server.sys.mapper.SysUserRoleMapper;
import com.bigstonecontainer.training.springboot.frm.server.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户业务接口实现类
 *
 * @author jiaodacailei
 */
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

    /**
     * 注入SysUserMapper
     */
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMapper roleMapper;



    /**
     * 用户列表查询
     *
     * @return 用户列表
     */
    @Override
    public List<SysUser> getList() {
        return sysUserMapper.getList();
    }

    /**
     * 获取单个用户信息
     *
     * @param id 用户ID
     * @return 单个用户信息
     */
    @Override
    public SysUser getOne(Long id) {

        List<Integer> roleis=  roleMapper.selectRoleListByUserId((id));
        SysUser sysUser = sysUserMapper.getOne(id);
        sysUser.setRoleIds(roleis);
        return sysUser;
    }

    /**
     * 创建一个用户
     *
     * @param user 用户
     * @return 插入的记录个数
     */
    @Override
    public int create(SysUser user) {
        return sysUserMapper.insert(user);
    }

    /**
     * 通过主键查找一个用户，并进行修改
     *
     * @param user 用户
     * @return 修改的记录个数
     */
    @Override
    public int updateById(SysUser user) {
        Integer userId = user.getUserId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        return sysUserMapper.updateById(user);
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user)
    {
        List <Integer> roles = user.getRoleIds();
        if (!ObjectUtils.isEmpty(roles))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Integer roleId : roles)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0)
            {
                userRoleMapper.batchUserRole(list);
            }
        }
    }
    /**
     * 在数据库中，通过主键查找一个用户，并进行删除
     *
     * @param id 用户ID
     * @return 删除的记录个数
     */
    @Override
    public int deleteById(Long id) {
        return sysUserMapper.deleteById(id);
    }
}
